package com.abe.libquick.base.mvp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.View;

import androidx.fragment.app.Fragment;

public class BaseBPresenter<V extends BaseView> extends BaseLogPresenter<V> {
    private Context context = null;

    protected Context context() {
        if (isActivity()) {
            context = (Activity) getView();
        } else if (isFragment()) {
            context = ((Fragment) getView()).getActivity();
        } else if (isDialog()) {
            context = ((Dialog) getView()).getContext();
        } else if (isFragment()) {
            context = ((View) getView()).getContext();
        }
        return context;
    }

    protected boolean isActivity() {
        if (!isViewAttached()) return false;
        return getView() instanceof Activity;
    }

    protected boolean isFragment() {
        if (!isViewAttached()) return false;
        return getView() instanceof Fragment;
    }

    protected boolean isDialog() {
        if (!isViewAttached()) return false;
        return getView() instanceof Dialog;
    }

    protected boolean isView() {
        if (!isViewAttached()) return false;
        return getView() instanceof View;
    }

    public abstract class SimLoadListener<T> extends SimResultListener<T> {

        private String Tag;
        private AlertDialog dialog;
        private boolean loadWithDialog;

        public SimLoadListener() {
            this(BaseBPresenter.this.getClass().getSimpleName());
        }

        public SimLoadListener(String tag) {
            this(tag, true);
        }

        public SimLoadListener(boolean loadWithDialog) {
            this(BaseBPresenter.this.getClass().getSimpleName(), loadWithDialog);
        }

        public SimLoadListener(String tag, boolean loadWithDialog) {
            this.Tag = tag;
            this.loadWithDialog = loadWithDialog;
        }

        public abstract void onData(T data);

        @Override
        public void onStart() {
            if (!isViewAttached()) return;
            if (context() == null) return;
            if (!loadWithDialog) return;
            dialog = new ProgressDialog(context());
            dialog.setMessage("Loading...");
            dialog.setCancelable(true);
            if (!dialog.isShowing()) {
                dialog.show();
            }
        }

        @Override
        public void onResult(T data) {
            if (!isViewAttached()) return;
            if (data != null) {
                onData(data);
            } else {
                onFail("MVP_PRESENTER_ERROR_DATA_NULL");
            }
        }

        @Override
        public void onFail(String msg) {
            if (!isViewAttached()) return;
            showError(msg);
            getView().onFail(Tag);
        }

        @Override
        public void onFinish() {
            if (!isViewAttached()) return;
            if (dialog != null) {
                dialog.dismiss();
            }
            getView().onFinish(Tag);
        }


    }

    public abstract class SimpleListener<T> implements ResultListener<T> {

        private String Tag;

        public SimpleListener() {
            this(BaseBPresenter.this.getClass().getSimpleName());
        }

        public SimpleListener(String tag) {
            Tag = tag;
        }

        public abstract void onData(T data);

        @Override
        public void onResult(T data) {
            if (!isViewAttached()) return;
            if (data != null) {
                onData(data);
            } else {
                onFail("MVP_PRESENTER_ERROR_DATA_NULL");
            }
        }

        @Override
        public void onFail(String msg) {
            if (!isViewAttached()) return;
            showError(msg);
            getView().onFail(Tag);
        }

        @Override
        public void onFinish() {
            if (!isViewAttached()) return;
            getView().onFinish(Tag);
        }
    }
}
